package com.woniuxy.renters.web.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.core.model.Result;
import com.woniuxy.core.util.JwtUtil;
import com.woniuxy.renters.dao.RentersAccount;
import com.woniuxy.renters.model.dto.RentersLoginDto;
import com.woniuxy.renters.model.form.AddrentersForm;
import com.woniuxy.renters.model.form.Renterslogin1Form;
import com.woniuxy.renters.model.form.RentersloginForm;
import com.woniuxy.renters.model.form.UpdatepassForm;
import com.woniuxy.renters.model.param.UpdatepassParam;
import com.woniuxy.renters.service.RentersAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 凌玉春
 * @since 2024年01月15日
 */
@RestController
@Api(
        tags = {"出租客账号相关接口"}
)
@RequestMapping("/rentersAccount")
public class RentersAccountController {
    @Autowired
    private RentersAccountService rentersAccountService;

    @ApiOperation("测试")
    @GetMapping("/test")
    public Result<String> test(){
        rentersAccountService.test();
        return Result.getResult(String.class).setMessage("测试成功");
    }


    @ApiOperation("出租客注册")
    @PostMapping({"/rentersRegister"})
    private Result rentersRegister(@RequestBody AddrentersForm form) throws Exception {
        this.rentersAccountService.rentersRegister(form);
        return Result.getResult().setMessage("出租客账号注册成功");
    }

    @ApiOperation("出租客注册发送短信")
    @GetMapping({"/rentersSend"})
    @ApiImplicitParam(name = "rentersPhone",value = "出租客电话")
    private Result rentersSend(String rentersPhone) throws Exception {
        this.rentersAccountService.rentersSend(rentersPhone);
        return Result.getResult().setMessage("出租课账号注册发送短信成功");
    }

    @ApiOperation("出租客修改密码")
    @PostMapping("/updatePassword")
    public Result updatePassword(@RequestBody UpdatepassForm upf, @RequestHeader String token){
        String id = JwtUtil.parseUserToken(token).get("id").toString();
        UpdatepassParam param = BeanUtil.toBean(upf, UpdatepassParam.class);
        param.setUserId(Integer.valueOf(id));
        rentersAccountService.updatePassword(param);
        return Result.getResult().setMessage("出租客修改密码成功");
    }

    @ApiOperation("出租客手机号登录发送短信")
    @GetMapping({"/rentersSend1"})
    @ApiImplicitParam(name = "rentersPhone",value = "出租客电话")
    private Result rentersSend1(String rentersInfoTel) throws Exception {
        this.rentersAccountService.rentersSend1(rentersInfoTel);
        return Result.getResult().setMessage("用户手机号登录发送短信成功");
    }

    @ApiOperation("出租客账号密码登录")
    @PostMapping({"/renterslogin"})
    private Result<RentersLoginDto> renterslogin(@RequestBody RentersloginForm rf, HttpServletResponse response) throws Exception {
        RentersLoginDto rentersLoginDto = this.rentersAccountService.renterslogin(rf);
        System.out.println("33333333333333333333"+rentersLoginDto.toString());
        HashMap<String, Object> map = new HashMap();
        map.put("id", rentersLoginDto.getRentersId());
        String token = JwtUtil.creatUserToken(map);
        response.setHeader("token", token);
        System.out.println(token);
        response.setHeader("Access-Control-Expose-Headers", "token");
        return Result.getResult(RentersLoginDto.class).setMessage("出租客账户密码登录成功");
    }

    @ApiOperation("出租客手机验证码登录")
    @PostMapping({"/login1"})
    private Result<RentersLoginDto> login1(@RequestBody Renterslogin1Form uf, HttpServletResponse response) throws Exception {
        RentersLoginDto rentersLoginDto = this.rentersAccountService.renterslogin1(uf);
        HashMap<String, Object> map = new HashMap();
        map.put("id", rentersLoginDto.getRentersId());
        String token = JwtUtil.creatUserToken(map);
        response.setHeader("token", token);
        System.out.println(token);
        response.setHeader("Access-Control-Expose-Headers", "token");
        return Result.getResult(RentersLoginDto.class).setMessage("出租客手机验证码登录成功");
    }

}

